<?
class Menu {
	private $html = '';
	private $class = '';
	private $parentId;
	private $pageId = array();
	private $pageTitle = array();
	private $pageLabel = array();
	private $administrator = false;
	
	function __construct($parent = '0', $class = 'menu', $hideView = false, $administrator = false) {
		global $_DB;
		
		if ($hideView == 'true') {
			$sql = "SELECT id, name, label FROM ".$_DB->getPrefix()."pages WHERE parent = '".$parent."' AND deleted = '0' ORDER BY sort";
		} else {
			$sql = "SELECT id, name, label FROM ".$_DB->getPrefix()."pages WHERE parent = '".$parent."' AND hidden = '0' AND deleted = '0' ORDER BY sort";
		}
		$r = $_DB->query($sql);

		foreach ($r as $page) {
			//$count++;
			//$this->html .= '<li'.$id_li.'>'.$img.'<a href="/'.$link[label].'.html">'.$link["title"].'</a></li>';
			$this->pageId[] = $page["id"];			
			
			
			$sql = "SELECT page.text
					FROM ".$_DB->getPrefix()."page_metatag as page, ".$_DB->getPrefix()."metatag as meta
					WHERE page.id_page = '".$page["id"]."' AND meta.id = page.id_meta AND meta.name = 'title'";
			
			$res = $_DB->query($sql);
			if (count($res) > 0){
				$this->pageTitle[] = $res[0]["text"];
			} else {
				$this->pageTitle[] = $page["name"];
			}
			
			$this->pageName[] = $page["name"];
			$this->pageLabel[] = $page["label"];	
		}
		$this->class = $class;
		$this->parentId = $parent;
		$this->administrator = $administrator;
	}

	function html() {
		global $_PAGE;	
		if (!empty($this->html)){
			return $this->html;
		}
	
		if (count($this->pageId) > 0){		
			$this->html = '<ul class="'.$this->class.'">';
			$i = 0;
			foreach ($this->pageId as $pId) {
				$page = new Page(null, $pId);
				$url = $page->getUrl();
	
				$class_span = ' class="menu-arrow menu-page-'.$this->pageId[$i].'" ';
				$class_li = '';
				if ($_PAGE->pId == $this->pageId[$i]) {
					$class_li = ' class="selected" ';
				} else {
					$class_li = ' class="no-selected" ';
				}
	
				$this->html .= '<li'.$class_li.'>';
				if ($this->administrator) {
					$this->html .= '<span'.$class_span.'></span>';
				}
				$this->html .= '<a href="'.$url.'" title="'.$this->pageTitle[$i].'" class="'.$this->pageLabel[$i].'"><span>'.$this->pageName[$i].'</span></a></li>';
				$i++;
			}
			$this->html .= '</ul>';
		}	
		return $this->html;
	}
	
	/*function html() {
		global $_PAGE;
		
		if (!empty($this->html)){
			return $this->html;
		}
		
		if (count($this->pageTitle) > 0){	
			//$_PAGE = new Page(null, $this->pageId[0]);
			
			$pathTree = '';
			if ($this->parentId > 0) {
				$pathTree .= $_PAGE->get('path');
				//$pathTree .= $_REQUEST["label"];
				$pathTree .= $_PAGE->get("label");
			}
			
			$this->html = '<ul class="'.$this->class.'">';
			$i = 0;
			foreach ($this->pageTitle as $title) {
				$uri = $pathTree.'/'.$this->pageLabel[$i];
				
				$class_span = ' class="menu-arrow menu-page-'.$this->pageId[$i].'" ';
				$class_li = '';
				if ($_PAGE->pId == $this->pageId[$i]) {
					$class_li = ' class="selected" ';
				} else {
					$class_li = ' class="no-selected" ';
				}
				
				$this->html .= '<li'.$class_li.'>';
				if ($this->administrator) {
					$this->html .= '<span'.$class_span.'></span>';
				}
				$this->html .= '<a href="'.$uri.'.html" title="'.$title.'" class="'.$this->pageLabel[$i].'"><span>'.$this->pageName[$i].'</span></a></li>';
				$i++;
			} 
			$this->html .= '</ul>';
		}
		return $this->html;
	}*/
}
?>
